//
//  LogisticsViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/23.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "LogisticsViewController.h"
#import "LogisticsStore.h"
#import "LogisticsCell.h"
#import "LogiticsHeaderView.h"
#import "OrderCenterList.h"
#import "OrderCenterModel.h"

@interface LogisticsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *listArr;
@property (nonatomic,strong) LogiticsHeaderView *headerView;

@end

@implementation LogisticsViewController

- (LogiticsHeaderView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[LogiticsHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 70)];
        _headerView.nameLa.text = [OrderCenterList sharedInstance].selectedModel.logisticsName;
        _headerView.numLa.text = [OrderCenterList sharedInstance].selectedModel.expressNumber;
    }
    
    return _headerView;
}

- (UITableView *)tableView{
        
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.5)];
        _tableView.estimatedRowHeight = 88;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.tableHeaderView = self.headerView;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.separatorInset = UIEdgeInsetsZero;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LogisticsCell *cell = [LogisticsCell tempWithTableView:tableView andIndexPath:indexPath];
    [cell setModel:self.listArr[indexPath.row] withRow:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.cell2_bottomline.alpha = 1;
    if (indexPath.row == self.listArr.count - 1) {
        
        cell.cell2_bottomline.alpha = 0;
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setNavBarWithTitle:@"物流管理"];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.bottom.equalTo(self.view);
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}


- (void)refresh{
    
    LogisticsStore *store = [[LogisticsStore alloc] init];
    
    NSString *kdName = [OrderCenterList sharedInstance].selectedModel.logisticsName;
    NSString *kdNum = [OrderCenterList sharedInstance].selectedModel.expressNumber;
    NSString *receiverPhone = [[OrderCenterList sharedInstance].selectedModel.phone substringFromIndex:7];

    MJWeakSelf
    [store getLogisticsInfoWithCom:kdName andNo:kdNum andKey:WuLiuAppKey andPhone:receiverPhone Success:^(NSArray *listArr) {

        self.listArr = listArr;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];

    } Failure:^(NSString *errorStr) {

        [self.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:errorStr];
    }];
}

@end

